﻿using System;
using System.Linq;
using System.Windows.Controls;

namespace EasySL.Controls
{
    public partial class ExDataGrid : DataGrid
    {
        private void BindSortEvent(ExCommandPanel cp)
        {
            cp.OnSort += (bindProperty, isAscend) =>
            {
                this.SortProperty = bindProperty;
                this.IsAscend = isAscend;
                
                if (isAscend)
                    currentDataRows = this.currentDataRows.OrderBy(r => r[bindProperty]).ToList();
                else
                    currentDataRows = this.currentDataRows.OrderByDescending(r => r[bindProperty]).ToList();

                this.SetDataSource(currentDataRows.Take(PageSize));
                this.SetCheckAll();
                this.ScrollBar.Init();

                foreach (var commandPanel in this.CommandPanels)
                    if (commandPanel.BindProperty != bindProperty)
                        commandPanel.IsAscend = null;
            };
        }
    }
}
